package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author Haoran Sun
 * @since 2024/07/15
 */

@RestController
@RequestMapping("/brand")
@Tag(name = "商品的品牌信息管理")
public class BrandController extends BaseController {

    @Resource
    private BrandService brandService;

    /**
     * 分页查询品牌列表
     */
    @Operation(summary = "分页查询品牌列表")
    @GetMapping("list")
    public TableDataInfo getBrandList(Brand brand) {
        return getDataTable(brandService.listBrand(brand));
    }

    /**
     * 获取全部品牌信息
     */
    @Operation(summary = "获取全部品牌")
    @GetMapping("getBrandAll")
    public AjaxResult getBrandAll() {
        return success(brandService.list());
    }

    /**
     * 新增品牌
     */
    @Operation(summary = "新增品牌")
    @PostMapping
    public AjaxResult addBrand(@RequestBody @Validated Brand brand) {
        return toAjax(brandService.save(brand));
    }
    /**
     * 修改品牌
     */
    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult updateBrand(@RequestBody @Validated Brand brand) {
        return toAjax(brandService.updateById(brand));
    }

    /**
     * 获取品牌的详细信息
     */
    @Operation(summary = "获取品牌的详细信息")
    @GetMapping("{id}")
    @RequiresPermissions("system:brand:query")
    public AjaxResult getBrandByDetailId(@PathVariable("id") Long id) {
        return success(brandService.getById(id));
    }

    /**
     * 批量删除品牌
     * @param ids 要删除的品牌id数组
     */
    @Operation(summary = "删除品牌")
    @DeleteMapping("{ids}")
    public AjaxResult deleteBrand(@PathVariable("ids") Long[] ids) {
        return toAjax(brandService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 获取全部品牌
     * @return 品牌列表
     */
    @InnerAuth
    @Operation(summary = "获取全部品牌")
    @GetMapping("getBrandAllList")
    public R<List<Brand>> getBrandAllList() {
        return R.ok(brandService.selectBrandAll());
    }

}
